/**
 * 
 */
package GUI.main.renderers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import GUI.helpers.ProjectConstants;

import officeserver.users.AccessLevel;
import officeserver.users.PersonalInfo;

/**
 * @author mramsey3
 * 
 */
public class UserListRenderer extends JPanel implements ListCellRenderer {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel patientLogoPanel;
    private JPanel nurseLogoPanel;
    private JPanel doctorLogoPanel;
    private JPanel adminLogoPanel;
    private JPanel textPanel;
    private JLabel nameLabel;
    private JLabel statusLabel;

    /**
     * 
     */
    public UserListRenderer() {
	setLayout(new BorderLayout());
	setBorder(new LineBorder(ProjectConstants.BORDER_SUB_COLOR, 2));
	add(getTextPanel(), BorderLayout.CENTER);
    }

    /**
     * @return
     */
    private JPanel getPaientLogoPanel() {
	if (patientLogoPanel == null) {
	    patientLogoPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.BORDER_SUB_COLOR);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36));
		    g.drawString("P", 12, getHeight() - 12);
		}
	    };
	    patientLogoPanel.setPreferredSize(new Dimension(50, 50));
	    patientLogoPanel.setBorder(BorderFactory.createLineBorder(
		    ProjectConstants.BORDER_SUB_COLOR, 6));
	    patientLogoPanel.setOpaque(false);
	}
	return patientLogoPanel;
    }

    /**
     * @return
     */
    private JPanel getNurseLogoPanel() {
	if(nurseLogoPanel == null){
	    nurseLogoPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.BORDER_SUB_COLOR);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36));
		    g.drawString("N", 12, getHeight() - 12);
		}
	    };
	    nurseLogoPanel.setPreferredSize(new Dimension(50, 50));
	    nurseLogoPanel.setBorder(BorderFactory.createLineBorder(
		    ProjectConstants.BORDER_SUB_COLOR, 6));
	    nurseLogoPanel.setOpaque(false);
	}
	return nurseLogoPanel;
    }

    /**
     * @return
     */
    private JPanel getDoctorLogoPanel() {
	if(doctorLogoPanel == null){
	    doctorLogoPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.BORDER_SUB_COLOR);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36));
		    g.drawString("D", 12, getHeight() - 12);
		}
	    };
	    doctorLogoPanel.setPreferredSize(new Dimension(50, 50));
	    doctorLogoPanel.setBorder(BorderFactory.createLineBorder(
		    ProjectConstants.BORDER_SUB_COLOR, 6));
	    doctorLogoPanel.setOpaque(false);
	}
	return doctorLogoPanel;
    }
    
    /**
     * @return
     */
    private JPanel getAdminLogoPanel(){
	if(adminLogoPanel == null){
	    adminLogoPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.BORDER_SUB_COLOR);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36));
		    g.drawString("A", 12, getHeight() - 12);
		}
	    };
	    adminLogoPanel.setPreferredSize(new Dimension(50, 50));
	    adminLogoPanel.setBorder(BorderFactory.createLineBorder(
		    ProjectConstants.BORDER_SUB_COLOR, 6));
	    adminLogoPanel.setOpaque(false);
	}
	return adminLogoPanel;
    }

    /**
     * @param type
     */
    private void setLogoPanel(AccessLevel type) {
	removeAll();
	switch (type) {
	case PATIENT:
	    add(getPaientLogoPanel(),BorderLayout.WEST);
	    break;
	case NURSE:
	    add(getNurseLogoPanel(),BorderLayout.WEST);
	    break;
	case DOCTOR:
	    add(getDoctorLogoPanel(),BorderLayout.WEST);
	    break;
	case ADMIN:
	    add(getAdminLogoPanel(),BorderLayout.WEST);
	}
	add(getTextPanel(),BorderLayout.CENTER);
    }

    /**
     * @return
     */
    private JPanel getTextPanel() {
	if (textPanel == null) {
	    textPanel = new JPanel();
	    textPanel.setLayout(new BorderLayout());
	    textPanel.setBorder(new MatteBorder(2,0,2,2,ProjectConstants.BORDER_SUB_COLOR));
	    textPanel.add(getNameLabel(), BorderLayout.CENTER);
	    textPanel.add(getStatusLabel(), BorderLayout.SOUTH);
	    textPanel.setOpaque(false);
	}
	return textPanel;
    }

    /**
     * @return
     */
    private JLabel getNameLabel() {
	if (nameLabel == null) {
	    nameLabel = new JLabel();
	    nameLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
	    nameLabel.setOpaque(false);
	}
	return nameLabel;
    }

    /**
     * @param text
     */
    private void setNameLabelText(String text) {
	getNameLabel().setText(text);
    }

    /**
     * @return
     */
    private JLabel getStatusLabel() {
	if (statusLabel == null) {
	    statusLabel = new JLabel();
	    statusLabel.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 12));
	    statusLabel.setOpaque(false);
	}
	return statusLabel;
    }

    /**
     * @param isActive
     */
    private void setStatusLabel(boolean isActive) {
	if (isActive) {
	    getStatusLabel().setText("  Active");
	} else {
	    getStatusLabel().setText("  Deactivated");
	}
    }

    public Component getListCellRendererComponent(JList list, Object value,
	    int arg2, boolean isSelected, boolean arg4) {
	PersonalInfo pInfo = (PersonalInfo) value;
	setLogoPanel(pInfo.getAccessLevel());
	setNameLabelText("  " + pInfo.getFirstName() + " "
		+ pInfo.getLastName());
	setStatusLabel(pInfo.isActive());
	if (isSelected) {
	    setBackground(list.getSelectionBackground());
	    setForeground(list.getSelectionForeground());
	    setBorder(new LineBorder(ProjectConstants.BORDER_MAIN_COLOR, 2));
	} else {
	    setBackground(list.getBackground());
	    setForeground(list.getForeground());
	    setBorder(new LineBorder(Color.white, 2));
	}
	setEnabled(list.isEnabled());
	setFont(list.getFont());
	setOpaque(true);
	setPreferredSize(new Dimension(getWidth(), 52));

	return this;
    }

}
